﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kitware.VTK;

namespace HAVTKPack
{
   public class RectilinearGridGeometry:Geometry
   {

       #region 构造函数

       public RectilinearGridGeometry(RectilinearGrid grid)
       {
           this._gridData = grid;
       }

       public RectilinearGridGeometry(RectilinearGrid grid, List<int[,]> pointUseSign)
       {
           this._gridData = grid;
           this._pointEffectiveSign = pointUseSign;
       }

       #endregion


      

     
       RectilinearGrid _gridData;
       /// <summary>
       /// 网格
       /// </summary>
       public RectilinearGrid GridData
       {
           get { return _gridData; }
           set {

               //外围赋值时触发
               bool isChanged = false;
               isChanged = IsRectilinearGridChanged .IsRecGridChanged (this._gridData, value);

               if (isChanged == true)
               {
                   _gridData = value;

                   this.OnRectilinearGridChanged();
               }
           
           }
       }

       List<int[,]> _pointEffectiveSign;
       /// <summary>
       /// 外围边框点的有效性标记
       /// </summary>
       private List<int[,]> PointEffectiveSign
       {
           get { return _pointEffectiveSign; }
           set {

               //构造函数或外围赋值时 触发
               bool isChanged = false;
               isChanged = IsRectilinearEffectiveChanged.IsRecPointEffectiveChanged (this._pointEffectiveSign, value);

               if (isChanged == true)
               {
                   _pointEffectiveSign = value;

                   this.OnPointEffectiveSignChanged();
               }

           }
       }


       #region VTK流水线所需要的对象

      [NonSerialized ]
       vtkRectilinearGrid _rgrid;
      // [NonSerialized ]
      //vtkFloatArray scalars ;



       #endregion


       public override void Render()
       {

           _rgrid = vtkRectilinearGrid.New();
           //_rgrid.SetDimensions(_gridData.XArray.Length, _gridData.YArray.Length, _gridData.ZArray .Length );//点个数

           vtkFloatArray xCoords = vtkFloatArray.New();
           for (int i = 0; i < this._gridData.XLength; i++)
           {
               xCoords.InsertNextValue(this._gridData.XArray[i]);
               
           }
           

           vtkFloatArray yCoords = vtkFloatArray.New();
           for (int j = 0; j < this._gridData.YLength; j++)
           {
               yCoords.InsertNextValue(this._gridData.YArray[j]);

           }


           vtkFloatArray zCoords = vtkFloatArray.New();
           for (int k = 0; k < this._gridData.ZLength; k++)
           {
               zCoords.InsertNextValue(this._gridData.ZArray[k]);
               
           }


           _rgrid.SetDimensions(this._gridData.XLength,this._gridData.YLength,this._gridData.ZLength);

           _rgrid.SetXCoordinates(xCoords);
           _rgrid.SetYCoordinates(yCoords);
           _rgrid.SetZCoordinates(zCoords);

           //vtkRectilinearGridGeometryFilter filter=vtkRectilinearGridGeometryFilter.New ();
           //filter.SetInput(this._rgrid);
           //filter.SetExtent(0, this._gridData.XLength,0,this._gridData.YLength,0,this._gridData.ZLength);
           //this._polyData = filter.GetOutput();

           //vtkExtractEdges edges = vtkExtractEdges.New();
           //edges.SetInput(filter.GetOutput());
           //vtkTubeFilter tube = vtkTubeFilter.New();
           //tube.SetInput(edges.GetOutput());
           //this._polyData = tube.GetOutput();


           vtkConnectivityFilter con = vtkConnectivityFilter.New();
           con.SetInput(this._rgrid);

           vtkGeometryFilter geo = vtkGeometryFilter.New();
           geo.SetInput(con.GetOutput());


           this._outPutData = geo.GetOutput();

          


         


       }


       public override void StopRender()
       {

           if (_rgrid!=null)
           {
               _rgrid.Dispose();
               
           }

       }

       #region 属性改变

       private void OnRectilinearGridChanged()
       {
           if (this._gridData == null)
           {
               return;
           }

           //this.GetPointsAndCellArray();

       }

       private void OnPointEffectiveSignChanged()
       {
           if (this._pointEffectiveSign == null)
           {
               return;
           }

           //this.GetPointsAndCellArray();

       }


       #endregion







   }
}
